﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;

namespace WebTools
{
    public partial class WXTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpHelper pageHelper = new HttpHelper();
            string timestamp = "8888";
            string token = txbToken.Text;
            string nonce = DateTime.Now.Ticks.ToString();
            string[] ArrTmp = { token, timestamp, nonce };
            Array.Sort(ArrTmp);
            string tmpStr = string.Join("", ArrTmp);
            tmpStr = EncriptHelper.EncryptSHA(tmpStr).ToLower();

            string strPostStr = txbURL.Text;
            if (!txbURL.Text.Contains("?"))
            {
                strPostStr = txbURL.Text + "?";
            }
            strPostStr = strPostStr + "&Type=" + txbEmail.Text.Trim() + "&token=" + token
                + "&timestamp=" + timestamp + "&signature=" + tmpStr + "&nonce=" + nonce + "&echostr=" + DateTime.Now.Ticks
               ;
            txbResult.Text = strPostStr;
            WebResponse response = pageHelper.doPost(strPostStr, txbSubmit.Text);
            string strRetunStr = pageHelper.ResponseToString(response);
            if (!string.IsNullOrWhiteSpace(strRetunStr))
            {
                txbResult.Text = strRetunStr;
            }
        }

        protected void rdType_SelectedIndexChanged(object sender, EventArgs e)
        {

            GetString();
        }
        private void GetString()
        {
            string p = "1";
            p = (rdType.SelectedIndex + 1).ToString();
            string strResult = "";
            switch (p)
            {
                case "1":
                    strResult = @"<xml>
<ToUserName><![CDATA[微信号]]></ToUserName>
<FromUserName><![CDATA[oNglrt3GvUl9Iat8Z-UnA-RsT9So]]></FromUserName>
<CreateTime>888888</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[{0}]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>";
                    break;
                case "2":
                    strResult = @"<xml>
<ToUserName><![CDATA[微信号]]></ToUserName>
<FromUserName><![CDATA[oNglrt3GvUl9Iat8Z-UnA-RsT9So]]></FromUserName>
<CreateTime>888888</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[CLICK]]></Event>
<EventKey><![CDATA[{0}]]></EventKey>
</xml>";
                    break;
                case "3":
                    strResult = @"<xml>
<ToUserName><![CDATA[微信号]]></ToUserName>
<FromUserName><![CDATA[oNglrt3GvUl9Iat8Z-UnA-RsT9So]]></FromUserName>
<CreateTime>888888</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[VIEW]]></Event>
<EventKey><![CDATA[{0}]]></EventKey>
</xml>";
                    break;
                case "4":
                    strResult = @"<xml>
<ToUserName><![CDATA[微信号]]></ToUserName>
<FromUserName><![CDATA[oNglrt3GvUl9Iat8Z-UnA-RsT9So]]></FromUserName>
<CreateTime>888888</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</xml>";
                    break;
                case "5":
                    strResult = @"<xml>
<ToUserName><![CDATA[微信号]]></ToUserName>
<FromUserName><![CDATA[oNglrt3GvUl9Iat8Z-UnA-RsT9So]]></FromUserName>
<CreateTime>888888</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[unsubscribe]]></Event>
</xml>";
                    break;
                case "6":
                    strResult = @"<xml>
<ToUserName><![CDATA[微信号]]></ToUserName>
<FromUserName><![CDATA[oNglrt3GvUl9Iat8Z-UnA-RsT9So]]></FromUserName>
<CreateTime>888888</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<PicUrl><![CDATA[{0}]]></PicUrl>
<MediaId><![CDATA[666666]]></MediaId>
<MsgId>1234567890123456</MsgId>
</xml>";
                    break;
                case "7":
                    strResult = @"<xml>
<ToUserName><![CDATA[微信号]]></ToUserName>
<FromUserName><![CDATA[oNglrt3GvUl9Iat8Z-UnA-RsT9So]]></FromUserName>
<CreateTime>888888</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<PicUrl><![CDATA[{0}]]></PicUrl>
<MediaId><![CDATA[666666]]></MediaId>
<MsgId>1234567890123456</MsgId>
</xml>";
                    break;
                case "8":
                    strResult = @"<xml>
<ToUserName><![CDATA[微信号]]></ToUserName>
<FromUserName><![CDATA[oNglrt3GvUl9Iat8Z-UnA-RsT9So]]></FromUserName>
<CreateTime>888888</CreateTime>
<MsgType><![CDATA[link]]></MsgType>
<Title><![CDATA[测试链接]]></Title>
<Description><![CDATA[这是个测试链接消息]]></Description>
<Url><![CDATA[{0}]]></Url>
<MsgId>1234567890123456</MsgId>
</xml> ";
                    break;
                //语音  
                //<xml>
                //<ToUserName><![CDATA[you]]></ToUserName>
                //<FromUserName><![CDATA[mochen]]></FromUserName>
                //<CreateTime>888888</CreateTime>
                //<MsgType><![CDATA[voice]]></MsgType>
                //<MediaId><![CDATA[ddd]]></MediaId>
                //<Format><![CDATA[amr]]></Format>
                //<MsgId>1234567890123456</MsgId>
                //</xml>
                //视频
                //                    <xml>
                //<ToUserName><![CDATA[you]]></ToUserName>
                //<FromUserName><![CDATA[mochen]]></FromUserName>
                //<CreateTime>888888</CreateTime>
                //<MsgType><![CDATA[video]]></MsgType>
                //<MediaId><![CDATA[ddd]]></MediaId>
                //<ThumbMediaId><![CDATA[999999]]></ThumbMediaId>
                //<MsgId>1234567890123456</MsgId>
                //</xml>
                //地理位置
                //                    <xml>
                //<ToUserName><![CDATA[you]]></ToUserName>
                //<FromUserName><![CDATA[mochen]]></FromUserName>
                //<CreateTime>888888</CreateTime>
                //<MsgType><![CDATA[location]]></MsgType>
                //<Location_X>3</Location_X>
                //<Location_Y>22222222222222222222222222222</Location_Y>
                //<Scale>20</Scale>
                //<Label><![CDATA[我在这儿]]></Label>
                //<MsgId>1234567890123456</MsgId>
                //</xml> 
            }
            txbSubmit.Text = string.Format(strResult, txbContent.Text);
        }

        protected void txbSubmit_TextChanged(object sender, EventArgs e)
        {
            GetString();
        }

        protected void txbSubmit_TextChanged1(object sender, EventArgs e)
        {

        }
    }
}